//
//  ViewController.m
//  table test
//
//  Created by tao故乡的云 on 2017/4/6.
//  Copyright © 2017年 故乡的云. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, weak) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:tableView];
    _tableView = tableView;
    tableView.contentInset = UIEdgeInsetsMake(20, 0, 44, 0);
    tableView.delegate = self;
    tableView.dataSource = self;
    _array = [NSMutableArray array];
    _array[0] = @"0";
    _array[1] = @"0";
    _array[2] = @"0";
    
    _tableView.editing = YES;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_array[section] integerValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = @"UITableViewCell";
    return cell;
}

#pragma mark - 折叠／展开
/// Header的生命周期短，reload时会释放，会重新创建一个新的header，
/// 所以需要保存header的数据状态，在创建时对其重新赋值
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 100, 30)];
    [btn addTarget:self action:@selector(showTable:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = section;
    [btn setTitle:[NSString stringWithFormat:@"分组%li",section] forState:UIControlStateNormal];
    btn.selected = [(NSString *)_array[section] isEqualToString: @"0"] ? NO : YES;
    if (btn.isSelected) {
        btn.backgroundColor = [UIColor yellowColor];
    }else {
        btn.backgroundColor = [UIColor greenColor];
    }

    return btn;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

- (void)showTable:(UIButton *)sw {
    sw.selected = !sw.isSelected;
    if (sw.isSelected) {
        sw.backgroundColor = [UIColor yellowColor];
        _array[sw.tag] = @"6";

    }else {
        sw.backgroundColor = [UIColor greenColor];
        _array[sw.tag] = @"0";
    }
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:sw.tag] withRowAnimation:UITableViewRowAnimationAutomatic];
}

#pragma mark - 移动
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (bool)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        return YES;
    }else {
        return NO;
    }
}

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSLog(@"source:%zi destination:%zi",sourceIndexPath.row,destinationIndexPath.row);
}
@end
